﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ThuVien;

namespace TranTekBE.Admin
{
    public partial class WebForm1 : PageRequireLogin
    {

        private int PageCount
        {
            set
            {
                ViewState["PageCount"] = value;
            }
            get
            {
                return FunctionsUtils.ConvertToInt(ViewState["PageCount"], 1);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
//            select  C003101 as BillNumber,
//    SUM(CASE WHEN C003100 like C003101 +'%' THEN 1 ELSE 0 END) AS RecordInBill,
//    (SELECT Count(NumItem) AS CoutItems
// from(select  COUNT(C003103)  as NumItem 
//    from T0031 where C003101 = DoanhSo.C003101
//         group by C003103) aaa) as ItemsSaleInBill
	
//from T0031 DoanhSo where C003100 like 'SGP-BH-20140430-0050%'
//and  UC00001 >= convert(datetime,'2013-04-30 00:00:00.001')
//and UC00001 <= convert(datetime,'2014-05-30 23:59:59.999')
//group by C003101
//ORDER BY ItemsSaleInBill desc


            

            ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "LoadJS", "LoadJS();", true);
            this.Page.Form.DefaultButton = btnSearch.UniqueID;


            //if (UserInfo.Admin == true)
            //{
                if (Page.IsPostBack == false)
                {
                    FunctionsUtils.GetDllStore(ddlStore,false);
                    txtFrom.Text = DateTime.Now.ToShortDateString();
                    txtTo.Text = DateTime.Now.ToShortDateString();
                    LoadData();
                   
                }
           // }
            //ltlTCSoDongTrenBill.Text = FunctionsUtils.CheckIPAllow().ToString();
        }



        



        private void LoadData()
        {
            //if (UserInfo.Admin == false)
            //{
            //    ltError.Text = "Bạn không đủ quyền để xem chức năng này !";
            //    return;
            //}

            ltError.Text = string.Empty;
            try
            {
                int currPage = FunctionsUtils.ConvertToInt(txtGoPage.Text, 1);
                int pageSize = FunctionsUtils.ConvertToInt(ddlPageSize.SelectedValue, FunctionsUtils.ConvertToInt(FunctionsUtils.GetConfigValue("PageSize"), 50));

                //string sSearchStore = this.CreateSearchConditionStore();
                //string sSearchDatetime = this.CreateSearchConditionDatetime();
                string sSortBy = this.SortBy();

                string sql = @"select  C003101 as ID_Bill,
                                    SUM(CASE WHEN C003100 like C003101 +'%' THEN 1 ELSE 0 END) AS RecordsInBill,
                                    (
                                        SELECT Count(NumItem) AS CoutItems
                                        from(
                                             select  COUNT(C003103)  as NumItem 
                                             from T0031 where C003101 = DoanhSo.C003101
                                             group by C003102) aaa
                                    ) as ItemsSaleInBill
	
                                from T0031 DoanhSo
                                where C003100 like '"+ ddlStore.SelectedValue.ToString() +@"%'
                                    and  UC00001 >= convert(datetime,'"+ txtFrom.Text+@" 00:00:00.001')
                                    and UC00001 <= convert(datetime,'" + txtTo.Text + @" 23:59:59.999')
                                group by C003101 " + sSortBy;
               

                DataSet ds = FunctionsUtils.Database.ExecuteQuery(sql);
               
                int recordCount = FunctionsUtils.ConvertToInt(ds.Tables[0].Rows.Count, 0);
                this.PageCount = FunctionsUtils.PageCount(recordCount, pageSize);

                currPage = FunctionsUtils.GetCurrPage(currPage, this.PageCount);
                ltPage.Text = string.Format("&nbsp;{0} of {1}&nbsp;", currPage, this.PageCount);
                txtGoPage.Text = currPage.ToString();

                SetButtonPageNavigationStatus(currPage, this.PageCount);

                //DataSet ds = FunctionsUtils.Database.ExecuteQuery(sSearch, currPage, pageSize);

                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    rpRevenue.DataSource = ds.Tables[0];
                    rpRevenue.DataBind();
                    ltNoRecord.Visible = false;

                    if (this.PageCount == 1)
                    {
                        phPageNavigation.Visible = false;
                    }
                    else
                    {
                        phPageNavigation.Visible = true;
                    }
                }
                else
                {
                    rpRevenue.DataSource = null;
                    rpRevenue.DataBind();
                    ltNoRecord.Visible = true;
                    phPageNavigation.Visible = false;
                }

                if (ViewState["TongSoDongTrenBill"] != null && ViewState["TongSoDongTrenBill"].ToString().Length > 0)
                    ltlTCSoDongTrenBill.Text = "<b>" + ViewState["TongSoDongTrenBill"].ToString() + "</b>";
                else
                    ltlTCSoDongTrenBill.Text = "<b>0</b>";

                if (ViewState["TongSoSanPhamBanTrenBill"] != null && ViewState["TongSoSanPhamBanTrenBill"].ToString().Length > 0)
                    ltlTCSoSanPhamBanTrenBill.Text = "<b>" + ViewState["TongSoSanPhamBanTrenBill"].ToString() + "</b>";
                else
                    ltlTCSoSanPhamBanTrenBill.Text = "<b>0</b>";

                ViewState["TongSoSanPhamBanTrenBill"] = "0";
                ViewState["TongSoDongTrenBill"] = "0";
                phPageNavigation.Visible = false;
            }
            catch (Exception ex)
            {
                ltError.Text = ex.Message;
                //log.WriteLog("Error in ProductMgmt (LoadProducts): {0} {1}", ex, ex.StackTrace);
            }
        }


        private void SetButtonPageNavigationStatus(int currPage, int pageCount)
        {
            if (currPage == 1)
            {
                btnPrev.Enabled = false;
            }
            else
            {
                btnPrev.Enabled = true;
            }

            if (currPage == pageCount)
            {
                btnNext.Enabled = false;
            }
            else
            {
                btnNext.Enabled = true;
            }
        }

        private string SortBy()
        {
            string SortBy = "ORDER BY {0} {1}";
            SortBy = string.Format(SortBy, ddlSortBy.SelectedValue, ddlSortDirect.SelectedValue);
            return SortBy;
        }

        protected void btnFilter_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        protected void Sort_Click(object sender, EventArgs e)
        {
            LinkButton lbt = sender as LinkButton;

            string CommandArgument = string.Empty;
            string css = string.Empty;
            if (lbt.CommandArgument.ToUpper() == "DESC")
            {
                CommandArgument = "ASC";
                css = "sortAsc";
            }
            else
            {
                CommandArgument = "DESC";
                css = "sortDesc";
            }


            //remove sort o cac link button khac
            this.RemoveSortCss();

            lbt.CommandArgument = CommandArgument;
            lbt.CssClass = "GridHeader " + css;
            //this.SortBy = lbt.CommandName;
            //this.SortDirect = CommandArgument;

            FunctionsUtils.SelectItemInControlList(ddlSortBy, lbt.CommandName, SelectBy.Value);
            FunctionsUtils.SelectItemInControlList(ddlSortDirect, CommandArgument, SelectBy.Value);

            LoadData();
        }

        protected void btnSearch_Click(object sender, ImageClickEventArgs e)
        {

        }

        protected void btnClearSearch_Click(object sender, ImageClickEventArgs e)
        {

        }

        public string TongSoDongTrenBill(object value)
        {
            if (ViewState["TongSoDongTrenBill"] != null && ViewState["TongSoDongTrenBill"].ToString().Length > 0)
                ViewState["TongSoDongTrenBill"] = FunctionsUtils.ConvertToDouble(ViewState["TongSoDongTrenBill"], 0) + FunctionsUtils.ConvertToDouble(value, 0);
            else
                ViewState["TongSoDongTrenBill"] = value;
            return value.ToString();
            
        }

        public string TongSoSanPhamBanTrenBill(object value)
        {
            if (ViewState["TongSoSanPhamBanTrenBill"] != null && ViewState["TongSoSanPhamBanTrenBill"].ToString().Length > 0)
                ViewState["TongSoSanPhamBanTrenBill"] = FunctionsUtils.ConvertToDouble(ViewState["TongSoSanPhamBanTrenBill"], 0) + FunctionsUtils.ConvertToDouble(value, 0);
            else
                ViewState["TongSoSanPhamBanTrenBill"] = value;
            return value.ToString();

        }

        protected void ddlStore_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}